package com.machine.dao.search.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.hibernate.Query;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;


import com.machine.bean.BetterSupportBean;
import com.machine.bean.RecommendBean;
import com.machine.dao.search.SearchDao;

@Component("SearchDao")
@Transactional
public class SearchDaoImpl implements SearchDao {

	private SessionFactory sessionfactory;

	public SessionFactory getSessionfactory() {
		return sessionfactory;
	}

	@Resource
	public void setSessionfactory(SessionFactory sessionfactory) {
		this.sessionfactory = sessionfactory;
	}

	
	@SuppressWarnings("unchecked")
	@Override
	public List<HashMap<String, String>> getKindNum() {
		String sql = "select kindname,kindnumber from componentkind where kindnumber%100=0";
		Session session = sessionfactory.getCurrentSession();
		SQLQuery query = session.createSQLQuery(sql);
		List<Object[]> names = query.list();
		
		List<HashMap<String, String>> list = new ArrayList<HashMap<String,String>>();
		for(Object[] obj : names){
			LinkedHashMap<String,String> map = new LinkedHashMap<String,String>();
			map.put(String.valueOf(obj[0]), String.valueOf(obj[1]));
			int temp = Integer.valueOf(String.valueOf(obj[1]));
			sql = "select kindname,kindnumber from componentkind where kindnumber > "+temp+" and kindnumber < "+temp+"+100";
		    session = sessionfactory.getCurrentSession();
			query = session.createSQLQuery(sql);
			List<Object[]> temps = query.list();
			for(Object[] objs : temps){
				map.put(String.valueOf(objs[0]), String.valueOf(objs[1]));
			}
			list.add(map);
		}
		return list;
	}

	@Override
	public Map<String, String> getAllKinds() {
		String sql = "select kindname,kindnumber from componentkind";
		Session session = sessionfactory.getCurrentSession();
		SQLQuery query = session.createSQLQuery(sql);
		List<Object[]> names = query.list();
		
		Map<String, String> map = new HashMap<String,String>();
		for(Object[] obj : names){
			
			map.put(String.valueOf(obj[0]), String.valueOf(obj[1]));
			
		}
		return map;
	}
	@Override
	public List<String> getLatestSearch() {
		Session session = sessionfactory.getCurrentSession();
		String sql = " select keywords from searchkeywords order by count desc limit 0,7;";
		Query query = session.createSQLQuery(sql);
		List<String> list = query.list();
		
		return list;

	}
	
	
	@Override
	public Map<String, String> getLasestSearch() {
		Session session = sessionfactory.getCurrentSession();
		String sql = " select keywords,count from searchkeywords order by count desc limit 0,10;";
		Query query = session.createSQLQuery(sql);
		List<Object[]> list = query.list();
		Map<String,String> map = new LinkedHashMap<String, String>();
		for(Object[] temp: list){
			map.put(String.valueOf(temp[0]), String.valueOf(temp[1]));
		}
		return map;
	}

	@Override
	public List<String> getPublisher() {
		Session session = sessionfactory.getCurrentSession();
		String sql = "select username from user where role = '供应商' order by registerTime desc limit 0,7";
		Query query = session.createSQLQuery(sql);
		List<String> list = query.list();
		return list;
	}

	@Override
	public List<String> getEnd() {
		Session session = sessionfactory.getCurrentSession();
		String sql = "select componentname,buyName from orderform order by outtime desc limit 0,20";
		Query query = session.createSQLQuery(sql);
		List<Object[]> list = query.list();
		List<String> lis = new ArrayList<String>();
		for(int i = 0; i < list.size(); i++){
			String t = "产品名： "+String.valueOf(list.get(i)[0])+"  买家:"+String.valueOf(list.get(i)[1]);
			lis.add(t);
			
		}
		return lis;
	}

	@Override
	public List<BetterSupportBean> getBetterSupport() {
		Session session = sessionfactory.getCurrentSession();
		String sql = "select id,componentname,dingdanhao,publishtime,gonghuoshang from goodgonghuoshang";
		Query query = session.createSQLQuery(sql);
		List<Object[]> tempList = query.list();
		List<BetterSupportBean> list = new ArrayList<BetterSupportBean>();
		for(Object[] t: tempList){
			BetterSupportBean bean = new BetterSupportBean();
			bean.setId(String.valueOf(t[0]));
			bean.setComponentname(String.valueOf(t[1]));
			bean.setDingdanhao(String.valueOf(t[2]));
			bean.setPublishtime(String.valueOf(t[3]));
			bean.setGonghuoshang(String.valueOf(t[4]));
			list.add(bean);
		}
		return list;
	}

	@Override
	public List<Map<RecommendBean, String>> getrecommended() {
		Session session = sessionfactory.getCurrentSession();
		String sql = "select id,name,pic from recommended_products";
		Query query = session.createSQLQuery(sql);
		List<Object[]> tempList = query.list();
		
		List<Map<RecommendBean,String>> list = new ArrayList<Map<RecommendBean,String>>();
		
		for(Object[] temp: tempList){
			RecommendBean bean = new RecommendBean();
			bean.setId(String.valueOf(temp[0]));
			bean.setName(String.valueOf(temp[1]));
			Map<RecommendBean,String> map = new HashMap<RecommendBean, String>();
			map.put(bean, String.valueOf(temp[2]));
			list.add(map);
		}
	//	System.out.println(map.size());
		return list;
	}

	@Override
	public Map<String,String> getAds() {
		Session session = sessionfactory.getCurrentSession();
		String sql = "select id,pic from ads";
		Query query = session.createSQLQuery(sql);
		List<Object[]> list = query.list();
		Map<String,String> map = new LinkedHashMap<String, String>();
		
		for(Object[] temp: list){
			map.put(String.valueOf(temp[0]), String.valueOf(temp[1]));
		}
		
		return map;
	}

	

	

}
